<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Examination extends MY_Controller
{
	public function __construct()
	{
		parent::__construct();
        $this->rate = 60;   //定义达标成绩
		$this->load->model('Examination_model', 'examination');
        $login_status  = $this->check_login_status();
        $this->accountID = $_SESSION['userInfo']['AccountID'];
        $this->departmentid = $_SESSION['userInfo']['departmentid'];
        if (empty($login_status)){
            redirect('/');die;
        }
	}
    public function index()
    {
        $_SESSION['sidebar'] = "examination";
        $departmentid =$this->departmentid;
        $accountID = $this->accountID;
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $doc_cat_list = $this->examination->get_cat_list();
        $limit = 20;
        $examination = $this->examination->getMyExaminationList($departmentid,$accountID,$per_page, $limit);
        $total_rows = $examination['count'];
        $base_url = '/examination/index?';
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);
        $examinationList = $examination['list'];
        $data = array(
            'examinationList'=>$examinationList,
            'page_links'=>$page_links,
            'per_page'=>$per_page,
            'doc_cat_list'=>$doc_cat_list,
            'departmentid'=>$departmentid
            );
        $this->load->view("examination/index", $data);
    }

    public function show(){
        $_SESSION['sidebar'] = "examination";
        $type = $this->input->get('type') ? $this->input->get('type') : 1;
        $paperID = $this->input->get('paperID') ? $this->input->get('paperID') : 0;
        $extID = $this->input->get('extID') ? $this->input->get('extID') : 0;
        $ifanswer = $this->examination->getMyScoreListCnt($this->accountID,$this->departmentid,$extID);
        if($ifanswer>0){
            $submitInfo = 0; //已答题 不可提交
        }else{
            $submitInfo =1; //未答题 可提交
        }
        $questionInfo = $this->examination->getQuestionInfo($type,$paperID);
        /*
         * 遍历题目进行模板拼接
         */
        $BasePath = APPPATH."smarty/templates/question/";
        $questionBody = $this->fetchSurveyTemp($questionInfo['questionInfo'],$BasePath);
        $questionTitle = $questionInfo['paperInfo'];
        $questionTitle['examinationID'] = $extID;
        $returnData = array('body'=>$questionBody,'info'=>$questionTitle,'submit'=>$submitInfo);
        $this->load->view("examination/show", $returnData);
    }
    public function answer(){
        $paperID = $this->input->post('id') ? $this->input->post('id') : 1;
        $answer = $this->input->post('data') ? $this->input->post('data') : 1;
        $extID = $this->input->post('extID') ? $this->input->post('extID') : 0;
        $departmentid =$this->departmentid;
        $accountID = $this->accountID;
        $questionAnser = $this->examination->getPaperAnswerInfo($paperID);
        //计算得分
        $score = $this->examination->getScoreByAnswer($answer,$questionAnser);
        //记录我的成绩
        $table = "tr_user_score";
        $params=array("examinationID"=>$extID,"paperID"=>$paperID,"departmentID"=>$departmentid,"userID"=>$accountID,"score"=>$score);
        $res = $this->examination->insertData($table,$params);
        echo $this->response_data(array('score'=>$score));
    }
    public function success(){
        $_SESSION['sidebar'] = "examination";
        $score = $this->input->get('score') ? $this->input->get('score') : 0;
        if($score<$this->rate){
            $notice = "未达标！";
        }else{
            $notice = "已达标！";
        }
        $data = array('score'=>$score,'notice'=>$notice);
        $this->load->view("examination/success", $data);
    }

    public function myscore(){
        $_SESSION['sidebar'] = "myexamination";
        $per_page = $this->input->get('per_page') ? $this->input->get('per_page') : 0;
        $limit = 11;
        $scoreList = $this->examination->getMyScoreList($this->accountID,$this->departmentid,$per_page,$limit);
        $total_rows = $this->examination->getMyScoreListCnt($this->accountID,$this->departmentid);
        $base_url = '/examination/myscore?';
        $page_links = $this->create_page_links($base_url,$total_rows,$limit);
        $data = array(
            'scoreList'=>$scoreList,
            'page_links'=>$page_links,
            'per_page'=>$per_page
        );
        $this->load->view("examination/myscore", $data);

    }

}
/* End of file Index.php */
/* Location: ./application/controllers/video.php */
